package com.ap.core.bulkupload.info.builder;

import java.io.InputStream;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ap.core.bulkupload.info.BulkUploadBaseInfo;
import com.ap.core.bulkupload.info.impl.BulkUploadMerchantRequestInfoImpl;
import com.ap.core.bulkupload.jaxb.domain.DistributorIdType;
import com.ap.core.bulkupload.jaxb.domain.FileHdrType;
import com.ap.core.bulkupload.jaxb.domain.Merchant;
import com.ap.core.bulkupload.jaxb.domain.Trailer;
import com.ap.core.bulkupload.listener.ParserListener;
import com.ap.core.bulkupload.service.XmlParserService;
import com.ap.core.exception.APCoreException;

/**
 * The Class BulkUploadMerchantRequestInfoBuilder.
 * @author Siva.Pochana
 */
public class BulkUploadMerchantRequestInfoBuilder implements BulkUploadInfoBuilder {

	/** The log. */
	private static final Logger LOG = LoggerFactory.getLogger(BulkUploadMerchantRequestInfoBuilder.class);
	
	/** The xml parser service. */
	private XmlParserService xmlParserService;

	/* (non-Javadoc)
	 * @see com.ap.core.bulkupload.info.builder.BulkUploadInfoBuilder#populateInfo(java.io.inputStream)
	 */
	@Override
	public BulkUploadBaseInfo populateInfo(InputStream inputStream) throws APCoreException {
		
		BulkUploadMerchantRequestInfoImpl bulkUploadRequestMerchantInfo = null;
		try {
			LOG.info("invoking xmlparserservice...");
		
			final BulkUploadMerchantRequestInfoBuilderProcessor bulkUploadRequestMerchantInfoBuilderProcessor = 
					new BulkUploadMerchantRequestInfoBuilderProcessor(inputStream);
			bulkUploadRequestMerchantInfo = bulkUploadRequestMerchantInfoBuilderProcessor.process();
			LOG.info("BulkUploadRequestMerchantInfoImpl is populated :"+bulkUploadRequestMerchantInfo.toString());
			
			return bulkUploadRequestMerchantInfo;
		
		} catch(APCoreException e) {
			LOG.error("Exception ocurrred ["+e.getMessage()+"]");
			throw e;
 		} catch (Exception e) {
			LOG.error("Exception ocurrred ["+e.getMessage()+"]");
			throw new APCoreException(e);			
		}
	}

	/**
	 * Sets the xml parser service.
	 *
	 * @param xmlParserService the xmlParserService to set
	 */
	public void setXmlParserService(XmlParserService xmlParserService) {
		this.xmlParserService = xmlParserService;
	}

  /**
   * The Class BulkUploadMerchantRequestInfoBuilderProcessor.
   */
  private class BulkUploadMerchantRequestInfoBuilderProcessor implements ParserListener{
	  
	  /** The input stream. */
  	private final InputStream inputStream;
	  
  	/** The total merchantcount. */
  	private int totalMerchantcount;
	  
  	/** The total trailer count. */
  	private int totalTrailerCount;
	  
	  /**
  	 * Instantiates a new bulk upload merchant request info builder processor.
  	 *
  	 * @param is the is
  	 */
  	BulkUploadMerchantRequestInfoBuilderProcessor(InputStream is){
		this.inputStream = is;  
	  }
	  
	  /**
  	 * Process.
  	 *
  	 * @return the bulk upload merchant request info impl
  	 * @throws APCoreException the aP core exception
  	 */
  	BulkUploadMerchantRequestInfoImpl process() throws APCoreException {
		BulkUploadMerchantRequestInfoImpl requestMerchantInfo = new BulkUploadMerchantRequestInfoImpl();
		
		/* Parse xml file */
		xmlParserService.parse(inputStream, this);
		
		/* set counter values */
		requestMerchantInfo.setTotalMerchantcount(totalMerchantcount);
		requestMerchantInfo.setTotalTrailerCount(totalTrailerCount);
		
		return requestMerchantInfo;
  		
	  }

	/* (non-Javadoc)
	 * @see com.ap.core.bulkupload.listener.ParserListener#onFileHeader(com.ap.core.bulkupload.jaxb.domain.FileHdrType)
	 */
	@Override
	public void onFileHeader(FileHdrType fileType) {
		/* Do nothing, as no requirement to read FileHeader */
	}

	/* (non-Javadoc)
	 * @see com.ap.core.bulkupload.listener.ParserListener#onDistributor(java.lang.String)
	 */
	@Override
	public void onDistributor(DistributorIdType distributorId) {
		/* Do nothing, as no requirement to read Distributor */
	}

	/* (non-Javadoc)
	 * @see com.ap.core.bulkupload.listener.ParserListener#onMerchant(com.ap.core.bulkupload.jaxb.domain.Merchant)
	 */
	@Override
	public void onMerchant(Merchant merchant) {
		totalMerchantcount = totalMerchantcount + 1;
	}

	/* (non-Javadoc)
	 * @see com.ap.core.bulkupload.listener.ParserListener#onTrailer(com.ap.core.bulkupload.jaxb.domain.Trailer)
	 */
	@Override
	public void onTrailer(Trailer trailer) {
		LOG.debug(" Trailer count :"+trailer.getTotalCtofRcds());
		this.totalTrailerCount = trailer.getTotalCtofRcds().intValue();
	}
	  
  }
}
